package com.mes.web.controller.cb;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mes.entity.cb.PageBean;
import com.mes.entity.cb.Plan;
import com.mes.entity.cb.PlanDetailed;
import com.mes.entity.jy.Plandetail;
import com.mes.service.cb.PlanDetailedService;
import com.mes.service.cb.PlanService;
import com.mes.service.jy.ProductPlanDetailServise;
import com.mes.utils.CustomUtils;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;

/**
 * @ClassName: PlanController 
 * @Description:采购
 * @Author cb
 * @Date 2017年12月20日 下午3:35:45 
 * @version 1.0
 */
@Controller
@RequestMapping("/plan")
public class PlanController {

	@Resource
	private PlanService planService;
	@Resource
	private PlanDetailedService planDetailedService;
	@Resource
	private ProductPlanDetailServise  productPlanDetailServise;
	
	@RequestMapping("/planList")
	public void planList(HttpServletRequest request,PageBean page,HttpServletResponse response,String limit,String offset) throws Exception {

		/*String planNumber = request.getParameter("planNumber"); 
		if(null != planNumber && !planNumber.equals("")){
			planNumber = new String(planNumber.getBytes("iso8859-1"),"utf-8");
			page.setPlanNumber(planNumber);
		}*/
		if (!"0".equals(offset)) {
			page.setPage(Integer.parseInt(offset)/Integer.parseInt(limit));
		}
		
		page.setPageSize(Integer.parseInt(limit));
		int total= planService.getTotal(page);
		List<Plan> plans = planService.planList(page);
		
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(plans, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
		
	}
	
	/*@RequestMapping("/PlanAll")
	public void PlanAll(HttpServletResponse response) throws Exception {
		List<Plan> plans=planService.PlanAll();
		Gson gson=new Gson();
		ResponseUtil.write(response, gson.toJson(plans));
	}*/
	
	@RequestMapping("/PlanAll2")
	public void PlanAll2(HttpServletResponse response,String limit,String offset,String remark14) throws Exception {
		PageBean page = CustomUtils.paging(limit, offset);
		page.setRemark14(remark14);
		List<Plan> plans=planService.PlanAll(page);
		
		int totas=planService.getPlanTotal();
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(plans, jsonConfig);
		jsonObject.put("total", totas);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	
	
	
	/**
	 * 采购计划添加
	 * @param plan 采购计划实体
	 * @param productionPlan 生产计划号
	 * @param datetime1 采购日期
	 * @param remark1s 到货日期集合
	 * @param materitids 物料id集合
	 * @param plantotals 计划数量
	 * @param ids  生产计划详情实体id集合
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/add")
	public void add(Plan plan,String productionPlan,String datetime1,String[] remark1s,String[] materitids,String[] plantotals, String ids ,HttpServletResponse response) throws Exception {
		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
		Date date =  formatter.parse(datetime1);
		
		plan.setDatetime(date);
		//生产计划单编号
		plan.setRemark9(productionPlan);
		//审批状态
		plan.setRemark6("0");
		//是否在采购订单中被选择
		plan.setRemark14("0");
		//关联生产计划详情ids
		plan.setRemark15(ids);
		Long id = planService.planAdd(plan);
		//添加采购计划单后更改生产计划单详情采购状态
		String[] idArr = ids.split(",");
		for (String ProductPlanDetailId : idArr) {
			Plandetail plandetail = new Plandetail();
			plandetail.setId(Long.valueOf(ProductPlanDetailId));
			plandetail.setRemartk13("1");
			productPlanDetailServise.editPlanDetailRemark13(plandetail );
			plandetail = null;
		}
		if (null != id && id >= 1) {
				for (int i = 0; i < materitids.length; i++) {
					PlanDetailed planDetailed =new PlanDetailed();
					planDetailed.setMateritid(Long.valueOf(materitids[i]));
					planDetailed.setPlantotal(plantotals[i]);
					//到货时间
					planDetailed.setRemark1(remark1s[i]);
					//是否在采购订单中被选择
					planDetailed.setRemark14("0");
					planDetailed .setPlannumber(plan.getPlannumber());
					Long idd = planDetailedService.planDetailedAdd(planDetailed);
				}
			
			response.getWriter().print("1");
		}else{
			response.getWriter().print("0");
		}
	}
	@RequestMapping("/update")
	public void update(Plan plan,String datetime1,String[] dids,String[] remark1s, HttpServletResponse response) throws Exception {
		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
		Date date =  formatter.parse(datetime1);
		plan.setDatetime(date);
		PageBean page = new PageBean();
		page.setPage(0);
		page.setPageSize(10);
		page.setId(plan.getId());
		List<Plan> plans = planService.planList(page);
		if(plans.size()== 1){
			Plan plan2 = new Plan();
			plan2 = plans.get(0);
			plan2.setDatetime(date);
			int id = planService.planEdit(plan2);
			if (id > 0) {
				for (int i = 0; i < dids.length; i++) {
					page.setId(Long.valueOf(dids[i]));
					List<PlanDetailed> planDetaileds = planDetailedService.PlanDetailedList(page);
					if (planDetaileds.size() == 1) {
						PlanDetailed planDetailed =planDetaileds.get(0);
						planDetailed.setId(Long.valueOf(dids[i]));
						planDetailed.setRemark1(remark1s[i]);
						int idd = planDetailedService.planDetailedEdit(planDetailed);
						planDetailed = null;
					}
				}
				
				response.getWriter().print("1");
			}else{
				response.getWriter().print("0");
			}
		}else{
			response.getWriter().print("-1");
		}
	}
	@RequestMapping("/toupdata")
	public String toupdata(Model model,HttpServletRequest request){
		PageBean pageBean = new PageBean();
		//页数
		pageBean.setPage(0);
		//每页条数
		pageBean.setPageSize(10);
		pageBean.setId(Long.valueOf((Integer.parseInt(request.getParameter("id")))));
		List<Plan> list = planService.planList(pageBean);
		if (list.size()>0) {
			model.addAttribute("plan", list.get(0));
		}
		return "cb/plan_update";
	}

	
	@RequestMapping("/delete")
	public void materieDemandDelete(String ids,HttpServletResponse response) throws Exception {
       
		//删除
		int n = planService.planDelete(ids);
		
		//删除成功
		if (n >= 1) {
			response.getWriter().print("1");
			//未成功
		} else {
			response.getWriter().print("0");
			
		}
        	
        
	}
	
}
